package com.bocai.dahuatech.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.Bean.PopBean;
import com.bocai.dahuatech.Bean.ProductListBean;
import com.bocai.dahuatech.Bean.ScreeBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.event.MainEvent;
import com.bocai.dahuatech.ui.adapter.FlowAdapter;
import com.bocai.dahuatech.ui.adapter.ProductGridAdapterHolder;
import com.bocai.dahuatech.ui.adapter.ProductListAdapterHolder;
import com.bocai.dahuatech.ui.view.FlowTagLayout;
import com.bocai.dahuatech.ui.view.LoadMoreRecyclerView;
import com.bocai.dahuatech.ui.view.MyListView;
import com.bocai.dahuatech.ui.view.OnTagSelectListener;
import com.bocai.dahuatech.utils.AESUtils;
import com.bocai.dahuatech.utils.PopUtil;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 产品三级（产品列表）
 */
public class ProductThirdActivity extends BaseActivity implements View.OnClickListener{
    @Bind(R.id.swipe_product_third)
    SwipeRefreshLayout swipeProduct;
    @Bind(R.id.swipe_product_third_grid)
    SwipeRefreshLayout swipeProductGrid;
    @Bind(R.id.lv_product_third)
    LoadMoreRecyclerView lvProductThird;
    @Bind(R.id.lv_product_third_grid)
    LoadMoreRecyclerView lvProductThirdGrid;
    @Bind(R.id.tv_productlist_screen)
    TextView tvScreen;
    @Bind(R.id.ll_product_third_screen)
    LinearLayout llScreen;
    @Bind(R.id.tv_productlist_sort)
    TextView tvSort;
    @Bind(R.id.tv_product_name)
    TextView tvName;
    @Bind(R.id.tv_product_count)
    TextView tvCount;
    private String title;
    private int fid;
    ProductListBean bean;
    int width;
    private ArrayList<PopBean> list;
    private PopUtil pu;
    PopupWindow mHeadPopWindow;
    FlowAdapter adapter;
    ProductListAdapterHolder productAdapter;
    ProductGridAdapterHolder productGridAdapter;
    int pager=1;
    String sortType="";
    String key="";
    String[] screeTypes;
    String sortpriceup,sortpricedown;
    String []typeTitels;
    List<ProductListBean.ContentEntity> datas=new ArrayList<>();
    ScreeBean screeBean=new ScreeBean();
    int screetype;
    private  boolean isSwitch=false;
    @Override
    protected int contentView() {
        return R.layout.activity_product_third;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        sortpriceup=getResources().getString(R.string.sortpriceup);
        sortpricedown=getResources().getString(R.string.sortpricedown);
        typeTitels= new String[]{sortpriceup, sortpricedown};
        DisplayMetrics dm=new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        width=dm.widthPixels;
        title=getIntent().getStringExtra("text");
        fid=getIntent().getIntExtra("fid", -1);
        key=getIntent().getStringExtra("key");
        screetype=getIntent().getIntExtra("screetype", 0);
        if (screetype==0)
        {
            llScreen.setVisibility(View.GONE);
        }else{
            llScreen.setVisibility(View.VISIBLE);
        }
        initToolbar(title);
        setSupportActionBar(mToolbar);
        lvProductThird.setLayoutManager(new LinearLayoutManager(this));
        lvProductThirdGrid.setLayoutManager(new GridLayoutManager(this,3));
//        view= LayoutInflater.from(this).inflate(R.layout.product_third_header,null);
//        tvName=(TextView)view.findViewById(R.id.tv_product_name);
//        tvCount=(TextView)view.findViewById(R.id.tv_product_count);
//        lvProductThird.addHeaderView(view);
        tvName.setText(title);
        getDatas(key);
        initEvent();
    }
    @Override
    public void onEventMainThread(MainEvent event)
    {
        super.onEventMainThread(event);
        if(event.msg.equals("goshopcart"))
        {
            finish();
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if(screetype==0){
            getMenuInflater().inflate(R.menu.menu_product_third_swich, menu);
        }else{

            getMenuInflater().inflate(R.menu.menu_product_third, menu);
        }
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case R.id.action_switch:
                if(!isSwitch){
                    item.setIcon(R.mipmap.swicth_list);
                    swipeProductGrid.setVisibility(View.VISIBLE);
                    swipeProduct.setVisibility(View.GONE);
                    isSwitch=true;

                }else{
                    item.setIcon(R.mipmap.swicth_grid);
                    swipeProductGrid.setVisibility(View.GONE);
                    swipeProduct.setVisibility(View.VISIBLE);
                    isSwitch=false;
                }
                break;
            case R.id.action_search:
                startActivity(new Intent(ProductThirdActivity.this, SearchProductActivity.class));
                break;
        }
        return false;
    }
    @Override
    protected void initEvent()
    {
        super.initEvent();
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        swipeProduct.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pager = 1;
//                key="";
                datas.clear();
                getDatas(key);
            }
        });

        lvProductThird.setLoadMoreListener(new LoadMoreRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {
                pager++;
                getDatas(key);
            }
        });
        swipeProductGrid.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pager = 1;
//                key="";
                datas.clear();
                getDatas(key);
            }
        });
        lvProductThirdGrid.setLoadMoreListener(new LoadMoreRecyclerView.LoadMoreListener() {
            @Override
            public void onLoadMore() {
                pager++;
                getDatas(key);
            }
        });
    }

    private void    getDatas(String key)
    {
        showLoading();
        getTestApi().getProductList(fid,screetype,sortType,key,pager,20)
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }
    Observer<BaseBean> observer=new Observer<BaseBean>()
    {
        @Override
        public void onCompleted()
        {

        }

        @Override
        public void onError(Throwable e)
        {
            hideLoading();
            showToast(R.string.neterror);
            swipeProduct.setRefreshing(false);
            swipeProductGrid.setRefreshing(false);
            Log.e("error",e.toString());
        }

        @Override
        public void onNext(BaseBean baseBean)
        {
            hideLoading();
            swipeProduct.setRefreshing(false);
            swipeProductGrid.setRefreshing(false);
            if(baseBean.getReturnNo().equals("1"))
            {
                String result = AESUtils.decode(baseBean.getContent());
                result = start + result + end;
                Log.e("result", result);
                bean = new Gson().fromJson(result, ProductListBean.class);
                if(bean.getContent().size()==0)
                {
                    showToast(R.string.nodata);
                }
                datas.addAll(bean.getContent());
                tvCount.setText(baseBean.getReturnInfo()+ getResources().getString(R.string.datacount));
                if(pager==1)
                {
                    productGridAdapter=new ProductGridAdapterHolder(ProductThirdActivity.this, bean.getContent());
                    lvProductThirdGrid   .setAdapter(productGridAdapter);
                    productAdapter = new ProductListAdapterHolder(ProductThirdActivity.this, bean.getContent());
                    lvProductThird .setAdapter(productAdapter);
                }else
                {
                    productAdapter.addDatas(bean.getContent());
                    productGridAdapter.addDatas(bean.getContent());
                }
                if(bean.getContent().size()<20)
                {
                    lvProductThird.notifyMoreFinish(false);
                    lvProductThirdGrid.notifyMoreFinish(false);
                }else
                {
                    lvProductThird.notifyMoreFinish(true);
                    lvProductThirdGrid.notifyMoreFinish(true);
                }
                tvScreen.setOnClickListener(ProductThirdActivity.this);
                tvSort.setOnClickListener(ProductThirdActivity.this);
            }else
            {
                showToast(baseBean.getReturnInfo());
            }

        }
    };
    private void showPopwindow(View v,final TextView tv,ArrayList<PopBean> list,int width,int height)
    {

        pu=PopUtil.getInstance(this,v,width,height,list);
        pu.setListener(new PopUtil.OnDatePopClickListener()
        {
            @Override
            public void onClick(PopUtil pu, PopBean date)
            {
                sortType=date.getId();
                pager=1;
                getDatas(key);
                pu.dismiss();
            }
        });
        pu.show();
    }
    String [] types={"0","1"};

    private ArrayList<PopBean> setSortList()
    {
        list=new ArrayList<>();
        for (int i=0;i<types.length;i++)
        {
            PopBean popBean=new PopBean();
            popBean.setId(types[i]);
            popBean.setName(typeTitels[i]);

            list.add(i,popBean);
        }

        return list;
    }
    MyListView lv_scree;
    TextView tvScreeCommit;
    private void showPopwindow()
    {
        try{
            View view = LayoutInflater.from(this).inflate(R.layout.pop_product_screen, null);
            tvScreeCommit=(TextView)view.findViewById(R.id.tv_scree_commit);
            tvScreeCommit.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    StringBuffer sb=new StringBuffer();
                    for (int i=0;i<screeTypes.length;i++)
                    {
                        if(screeTypes[i]!=null)
                        {
                            sb.append(screeTypes[i]);
                            sb.append("★");
                        }
                    }
                    mHeadPopWindow.dismiss();
                    Log.e("scree", sb.toString());
                    pager=1;
                    getDatas(sb.substring(0, sb.length() - 1));
//                    Toast.makeText(ProductThirdActivity.this, sb.substring(0, sb.length() - 1), Toast.LENGTH_SHORT).show();
                    Log.e("scree",sb.substring(0, sb.length()-1));
                }
            });
            lv_scree=(MyListView)view.findViewById(R.id.lv_scree);
            mHeadPopWindow = new PopupWindow(view, width, ViewGroup.LayoutParams.WRAP_CONTENT);
            mHeadPopWindow.setFocusable(true);
            mHeadPopWindow.setOutsideTouchable(true);
            // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
            mHeadPopWindow.setBackgroundDrawable(new BitmapDrawable());
            WindowManager.LayoutParams lp = getWindow().getAttributes();
            lp.alpha = 0.7f;
            getWindow().setAttributes(lp);
            mHeadPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener()
            {
                @Override
                public void onDismiss()
                {
                    WindowManager.LayoutParams lp = getWindow().getAttributes();
                    lp.alpha = 1f;
                    getWindow().setAttributes(lp);
                }
            });
            mHeadPopWindow.setBackgroundDrawable(new BitmapDrawable());
            mHeadPopWindow.showAsDropDown(tvScreen, 0, 0);
            lv_scree.setAdapter(new ScreeAdapter(this,screeBean));
        }catch (Exception e)
        {
            e.printStackTrace();

        }
    }
    private void initIPCData()
    {
        String data="{\"content\":[{\"title\":\"外观\",\"titletype\":\"Fappearance\",\"type\":[{\"type\":\"枪机\"},{\"type\":\"半球\"}]},{\"title\":\"红外灯数量\",\"titletype\":\"FLEDNumber\",\"type\":[{\"type\":\"单灯\"},{\"type\":\"双灯\"},{\"type\":\"四灯\"},{\"type\":\"六灯\"}]},{\"title\":\"最大分辨率\",\"titletype\":\"FMAXResolution\",\"type\":[{\"type\":\"100W\"},{\"type\":\"130W\"},{\"type\":\"200W\"},{\"type\":\"400W\"}]},{\"title\":\"星光\",\"titletype\":\"Fstarlight\",\"type\":[{\"type\":\"支持\"},{\"type\":\"不支持\"}]},{\"title\":\"视频编码标准\",\"titletype\":\"FVideoCodingStandards\",\"type\":[{\"type\":\"H.265\"},{\"type\":\"H.264\"}]},{\"title\":\"宽动态\",\"titletype\":\"FWideDynamic\",\"type\":[{\"type\":\"物理宽动态\"},{\"type\":\"数字宽动态\"}]},{\"title\":\"透雾\",\"titletype\":\"FThroughFog\",\"type\":[{\"type\":\"支持\"},{\"type\":\"不支持\"}]},{\"title\":\"强光抑制\",\"titletype\":\"FStrongLightInhibited\",\"type\":[{\"type\":\"支持\"},{\"type\":\"不支持\"}]},{\"title\":\"智能功能\",\"titletype\":\"FSmartFunctions\",\"type\":[{\"type\":\"支持\"},{\"type\":\"不支持\"}]},{\"title\":\"音频\",\"titletype\":\"Faudio\",\"type\":[{\"type\":\"支持\"},{\"type\":\"不支持\"}]},{\"title\":\"报警\",\"titletype\":\"Falarm\",\"type\":[{\"type\":\"支持\"},{\"type\":\"不支持\"}]},{\"title\":\"内置MIC\",\"titletype\":\"FBuiltInMic\",\"type\":[{\"type\":\"支持\"},{\"type\":\"不支持\"}]},{\"title\":\"Micro SD卡\",\"titletype\":\"FMicroSD\",\"type\":[{\"type\":\"支持\"},{\"type\":\"不支持\"}]},{\"title\":\"WiFi\",\"titletype\":\"FWiFi\",\"type\":[{\"type\":\"支持\"},{\"type\":\"不支持\"}]},{\"title\":\"供电方式\",\"titletype\":\"FPowerSupply\",\"type\":[{\"type\":\"POE\"},{\"type\":\"DC12V\"}]},{\"title\":\"镜头类型\",\"titletype\":\"FLensType\",\"type\":[{\"type\":\"定焦\"},{\"type\":\"变焦\"}]}]}";
        screeBean=new Gson().fromJson(data,ScreeBean.class);
        screeTypes=new String[screeBean.getContent().size()];
    }
    private void initHACData()
    {
        String data="{\"content\":[{\"title\":\"外观\",\"titletype\":\"Fappearance\",\"type\":[{\"type\":\"枪机\"},{\"type\":\"半球\"}]},{\"title\":\"最大分辨率\",\"titletype\":\"FMAXResolution\",\"type\":[{\"type\":\"100W\"},{\"type\":\"130W\"},{\"type\":\"200W\"}]},{\"title\":\"镜头类型\",\"titletype\":\"FLensType\",\"type\":[{\"type\":\"定焦\"},{\"type\":\"变焦\"}]},{\"title\":\"红外灯数量\",\"titletype\":\"FLEDNumber\",\"type\":[{\"type\":\"单灯\"},{\"type\":\"双灯\"},{\"type\":\"圈灯\"},{\"type\":\"非红外\"}]},{\"title\":\"内置MIC\",\"titletype\":\"FBuiltInMic\",\"type\":[{\"type\":\"支持\"},{\"type\":\"不支持\"}]}]}";
        screeBean=new Gson().fromJson(data,ScreeBean.class);
        screeTypes=new String[screeBean.getContent().size()];
    }
    private void initNVRData()
    {
        String data="{\"content\":[{\"title\":\"视频输入\",\"titletype\":\"FVideoInput\",\"type\":[{\"type\":\"4路\"},{\"type\":\"8路\"},{\"type\":\"16路\"},{\"type\":\"32路\"}]},{\"title\":\"编码类型\",\"titletype\":\"FCodingType\",\"type\":[{\"type\":\"H.264\"},{\"type\":\"H.265\"}]},{\"title\":\"支持分辨率\",\"titletype\":\"Fresolution\",\"type\":[{\"type\":\"200W\"},{\"type\":\"600W\"},{\"type\":\"1200W\"}]},{\"title\":\"接入带宽\",\"titletype\":\"FIncomingBandwidth\",\"type\":[{\"type\":\"24Mbps\"},{\"type\":\"28Mbps\"},{\"type\":\"56Mbps\"},{\"type\":\"64Mbps\"},{\"type\":\"128Mbps\"},{\"type\":\"200Mbps\"},{\"type\":\"256Mbps\"}]},{\"title\":\"音频输入\",\"titletype\":\"FAudioInput\",\"type\":[{\"type\":\"不支持\"},{\"type\":\"1路\"}]},{\"title\":\"音频输出\",\"titletype\":\"FAudioOutput\",\"type\":[{\"type\":\"不支持\"},{\"type\":\"1路\"}]},{\"title\":\"报警输入输出\",\"titletype\":\"Falarm\",\"type\":[{\"type\":\"不支持\"},{\"type\":\"4进2出\"},{\"type\":\"8进3出\"},{\"type\":\"16进4出\"},{\"type\":\"16进6出\"}]},{\"title\":\"视频输出\",\"titletype\":\"FVideoOutput\",\"type\":[{\"type\":\"1*VGA+1*HDMI\"},{\"type\":\"1*VGA+2*HDMI\"}]},{\"title\":\"网络接口\",\"titletype\":\"FNetworkInterface\",\"type\":[{\"type\":\"1个\"},{\"type\":\"2个\"}]},{\"title\":\"网口性能\",\"titletype\":\"FNetPortPerformance\",\"type\":[{\"type\":\"百兆网口\"},{\"type\":\"千兆网口\"}]},{\"title\":\"PoE网口\",\"titletype\":\"FPoE\",\"type\":[{\"type\":\"无\"},{\"type\":\"4个\"},{\"type\":\"8个\"},{\"type\":\"16个\"}]},{\"title\":\"串行接口\",\"titletype\":\"FSerialInterface\",\"type\":[{\"type\":\"1个RS232\"},{\"type\":\"1个RS232 +1个 RS-485\"}]},{\"title\":\"USB\",\"titletype\":\"FUSB\",\"type\":[{\"type\":\"2个\"},{\"type\":\"3个\"},{\"type\":\"4个\"}]},{\"title\":\"盘位数\",\"titletype\":\"FDiskNumber\",\"type\":[{\"type\":\"1\"},{\"type\":\"2\"},{\"type\":\"4\"},{\"type\":\"8\"}]},{\"title\":\"机箱高度\",\"titletype\":\"Fdimension\",\"type\":[{\"type\":\"Smart 1U\"},{\"type\":\"Compact 1U\"},{\"type\":\"1U\"},{\"type\":\"1.5U\"},{\"type\":\"2U\"}]},{\"title\":\"解码能力\",\"titletype\":\"FDecodingAbility\",\"type\":[{\"type\":\"2*1080P\"},{\"type\":\"4*1080P\"},{\"type\":\"6*1080P\"},{\"type\":\"10*1080P\"}]}]}";
        screeBean=new Gson().fromJson(data,ScreeBean.class);
        screeTypes=new String[screeBean.getContent().size()];
    }
    private void initmonitorData() {
        String data="{\"content\":[{\"title\":\" 系列\",\"titletype\":\"FSeries\",\"type\":[{\"type\":\"S200\"},{\"type\":\"S400\"},{\"type\":\"S600\"}]},{\"title\":\" 面板尺寸\",\"titletype\":\"FPanelSize\",\"type\":[{\"type\":\"19″\"},{\"type\":\"22″\"},{\"type\":\"27″\"},{\"type\":\"28″\"},{\"type\":\"32″\"},{\"type\":\" 42″\"},{\"type\":\" 43″\"},{\"type\":\" 49″\"},{\"type\":\"55″\"},{\"type\":\"  65″\"},{\"type\":\"  70″\"},{\"type\":\"  84″\"}]},{\"title\":\" 屏幕比例\",\"titletype\":\"FScreenScale\",\"type\":[{\"type\":\" 4:3\"},{\"type\":\" 16:9\"}]},{\"title\":\" 亮度\",\"titletype\":\"FBrightness\",\"type\":[{\"type\":\" 250cd/m2\"},{\"type\":\" 300cd/m²\"},{\"type\":\" 350cd/m²\"},{\"type\":\" 450cd/m2\"},{\"type\":\" 500cd/m2\"},{\"type\":\" 600cd/m2\"},{\"type\":\" 700cd/m2\"}]},{\"title\":\" 对比度\",\"titletype\":\"FContrast\",\"type\":[{\"type\":\" 1000:1\"},{\"type\":\" 1100:1\"},{\"type\":\" 1200:1\"},{\"type\":\" 1600:1\"},{\"type\":\" 3500:1\"},{\"type\":\" 4000:1\"},{\"type\":\" 4500:1\"},{\"type\":\" 5000:1\"}]},{\"title\":\" 分辨率\",\"titletype\":\"FResolution\",\"type\":[{\"type\":\" 1280×1024\"},{\"type\":\" 1366×768\"},{\"type\":\" 1920×1080\"},{\"type\":\" 3840×2160\"}]},{\"title\":\" 可视角度(水平/垂直)\",\"titletype\":\"FVisibleAngle\",\"type\":[{\"type\":\" 170°/160°\"},{\"type\":\" 176°/176°\"},{\"type\":\" 178°/178°\"}]},{\"title\":\" 控制方式\",\"titletype\":\"FControlMode\",\"type\":[{\"type\":\" 按键控制，红外控制\"},{\"type\":\" 按键控制\"},{\"type\":\" RS232串口控制，红外遥控，按键控制\"},{\"type\":\" RS232串口控制，红外遥控\"}]},{\"title\":\" 输入标配\",\"titletype\":\"FStandardInput\",\"type\":[{\"type\":\"RS232\"},{\"type\":\"USB\"},{\"type\":\"CVBS(BNC)\"},{\"type\":\"DVI\"},{\"type\":\"音频\"}]},{\"title\":\"输出标配\",\"titletype\":\"FStandardOutput\",\"type\":[{\"type\":\"CVBS（BNC）\"},{\"type\":\"内置喇叭\"},{\"type\":\"RS232\"}]}]}";
        screeBean=new Gson().fromJson(data,ScreeBean.class);
        screeTypes=new String[screeBean.getContent().size()];
    }
    private void initHCVRData()
    {
        String data="{\"content\":[{\"title\":\"预览最大分辨率\",\"titletype\":\"FPreviewMaxResolution\",\"type\":[{\"type\":\"720P\"},{\"type\":\"1080P\"}]},{\"title\":\"编码最大分辨率\",\"titletype\":\"FCodingMaxResolution\",\"type\":[{\"type\":\"960H实时\"},{\"type\":\"720P实时\"},{\"type\":\"1080N实时\"},{\"type\":\"1080P实时\"},{\"type\":\"1080P非实时\"},{\"type\":\"720P非实时\"}]},{\"title\":\"最大回放能力\",\"titletype\":\"FMAXPlayback\",\"type\":[{\"type\":\"4路\"},{\"type\":\"8路\"},{\"type\":\"16路\"}]},{\"title\":\"模拟接入能力\",\"titletype\":\"FAnalogAccessCapability\",\"type\":[{\"type\":\"4路\"},{\"type\":\"8路\"},{\"type\":\"16路\"},{\"type\":\"32路\"}]},{\"title\":\"最大网络接入能力\",\"titletype\":\"FMaxNetworkAccessCapability\",\"type\":[{\"type\":\"2路8Mbps\"},{\"type\":\"4路16Mbps\"},{\"type\":\"8路160Mbps\"},{\"type\":\"16路160Mbps\"},{\"type\":\"24路160Mbps\"},{\"type\":\"32路160Mbps\"},{\"type\":\"6路24Mbps\"},{\"type\":\"12路48Mbps\"},{\"type\":\"24路96Mbps\"}]},{\"title\":\"外部音频输入\",\"titletype\":\"FAudioInput\",\"type\":[{\"type\":\"1路\"},{\"type\":\"4路\"},{\"type\":\"16路\"}]},{\"title\":\"音频输出\",\"titletype\":\"FAudioOutput\",\"type\":[{\"type\":\"1路RCA\"},{\"type\":\"1路BNC\"}]},{\"title\":\"报警输入\",\"titletype\":\"FAlarmInput\",\"type\":[{\"type\":\"8路\"},{\"type\":\"16路\"},{\"type\":\"无\"}]},{\"title\":\"报警输出\",\"titletype\":\"FAlarmOutput\",\"type\":[{\"type\":\"3路\"},{\"type\":\"6路\"},{\"type\":\"无\"}]},{\"title\":\"盘位数\",\"titletype\":\"FDiskNumber\",\"type\":[{\"type\":\"1个\"},{\"type\":\"2个\"},{\"type\":\"4个\"},{\"type\":\"8个\"}]},{\"title\":\"机箱高度\",\"titletype\":\"Fdimension\",\"type\":[{\"type\":\"Compact 1U\"},{\"type\":\"1U\"},{\"type\":\"1.5U\"},{\"type\":\"2U\"}]}]}";
        screeBean=new Gson().fromJson(data,ScreeBean.class);
        screeTypes=new String[screeBean.getContent().size()];
    }
    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.tv_productlist_screen:
                if(screetype!=0){
                    //0-不支持高级筛选  1- HDCVI摄像机  2- HDCVI数字硬盘录像机  3- NVR  4-网络摄像机
//                    key="";
                    if(screetype==1)
                    {
                        initHACData();
                        showPopwindow();
                    }else if(screetype==2)
                    {
                        initHCVRData();
                        showPopwindow();
                    }else if(screetype==3)
                    {
                        initNVRData();
                        showPopwindow();
                    }else if(screetype==4)
                    {
                        initIPCData();
                        showPopwindow();
                    }else if(screetype==5)
                    {
                        initmonitorData();
                        showPopwindow();
                    }

                }else  if(screetype==0)
                {
                    showToast(R.string.noscreen);
                }
                break;
            case R.id.tv_productlist_sort:
                showPopwindow(tvSort,tvSort,setSortList(),width,20);
                break;
        }
    }
    class ScreeAdapter extends BaseAdapter
    {
        Context context;
        ScreeBean screebean;
        ViewHolder viewHolder=null;
        public ScreeAdapter(Context context,ScreeBean bean)
        {
            this.context=context;
            this.screebean=bean;
        }
        @Override
        public int getCount()
        {
            return screebean.getContent().size();
        }

        @Override
        public Object getItem(int position)
        {
            return screebean.getContent().get(position);
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {

            if(convertView==null)
            {
                viewHolder=new ViewHolder();
                convertView=LayoutInflater.from(context).inflate(R.layout.scree_item,null);
                viewHolder.ftl_screen=(FlowTagLayout)convertView.findViewById(R.id.ftl_screen);
                viewHolder.tvTitle=(TextView)convertView.findViewById(R.id.tv_scree_title);
                viewHolder.tvContent=(TextView)convertView.findViewById(R.id.tv_scree_content);
                viewHolder.ftl_screen.setOnTagSelectListener(new MyOnTagSelectListener(position,viewHolder.tvContent));
                convertView.setTag(viewHolder);
            }else
            {
                viewHolder= (ViewHolder) convertView.getTag();
            }
            viewHolder.tvTitle.setText(screebean.getContent().get(position).getTitle()+":");
            viewHolder.tvContent.setText(getResources().getText(R.string.all));
            viewHolder.ftl_screen.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_MULTI);
            adapter=new FlowAdapter<>(context);
            viewHolder.ftl_screen.setAdapter(adapter);
            initscreedatas(position);
            return convertView;
        }
        class  MyOnTagSelectListener implements OnTagSelectListener
        {
            private int position;
            private TextView tvContent;
            public MyOnTagSelectListener(int position,TextView tvContent)
            {
                this.position=position;
                this.tvContent=tvContent;
            }
            //key=”Fappearance☆枪机★FMAXResolution☆100W”

            @Override
            public void onItemSelect(FlowTagLayout parent, List<Integer> selectedList)
            {
                if (selectedList != null && selectedList.size() > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    StringBuilder sbmore = new StringBuilder();
                    for (int i : selectedList)
                    {
                        sb.append(parent.getAdapter().getItem(i));
                        sb.append("/");
                        sbmore.append(parent.getAdapter().getItem(i));
                        sbmore.append(",");
                    }

                    tvContent.setText(sb.substring(0, sb.length() - 1));
//                    stringBuffer=screebean.getContent().get(position).getTitletype()+"☆"+stringBuffer.append(sb.substring(0, sb.length() - 1));
                    screeTypes[position]=screebean.getContent().get(position).getTitletype()+"☆"+sbmore.substring(0, sb.length() - 1);
                } else
                {
                    tvContent.setText(getResources().getText(R.string.all));
                    screeTypes[position]=null;
                }
            }
        }
        private void initscreedatas(int position)
        {
            List<String> dataSource = new ArrayList<>();
            for (int i=0;i<screebean.getContent().get(position).getType().size();i++)
            {

                dataSource.add(screebean.getContent().get(position).getType().get(i).getType());
            }
            adapter.onlyAddAll(dataSource);
        }

        class ViewHolder
        {
            FlowTagLayout  ftl_screen;
            TextView tvTitle,tvContent;
        }
    }
}
